package com.qu.tabhost.tabs;

import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.qu.tabhost.R;
import com.qu.tabhost.tabs.widget.MainTab;
import com.qu.tabhost.tabs.widget.MyFragmentTabHost;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TabHostActivity extends AppCompatActivity implements View.OnTouchListener,
            View.OnClickListener,TabHost.OnTabChangeListener{
    @BindView(android.R.id.tabhost)
    MyFragmentTabHost tabHost;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_host);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        tabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);
        initTabs();
        //初始化
        tabHost.setCurrentTab(0);
        tabHost.setOnTabChangedListener(this);
    }
    //对tab进行初始化
    private void initTabs(){
        MainTab[] tabs = MainTab.values();
        int count = tabs.length;
        for(int i=0;i<count;i++){
            MainTab mainTab = tabs[i];
            TabHost.TabSpec tab = tabHost.newTabSpec(getString(mainTab.getTitle()));
            View indicator = View.inflate(this,R.layout.tab_indicator,null);
            ImageView icon = (ImageView) indicator.findViewById(R.id.iv_icon);
            TextView  title = (TextView) indicator.findViewById(R.id.tab_title);
            Drawable drawable = this.getResources().getDrawable(mainTab.getIcon());
            icon.setImageDrawable(drawable);
            if(i == 2){
                indicator.setVisibility(View.INVISIBLE);
                tabHost.setmNoTagChanged(getString(mainTab.getTitle()));
            }
            title.setText(getString(mainTab.getTitle()));
            tab.setIndicator(indicator);
            tab.setContent(new TabHost.TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                    return new View(TabHostActivity.this);
                }
            });
            tabHost.addTab(tab,mainTab.getClx(),null);
            tabHost.getTabWidget().getChildAt(i).setOnTouchListener(this);
        }
    }

    @OnClick(R.id.quick_option_iv)
    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.quick_option_iv){
            Toast.makeText(TabHostActivity.this,"添加菜单...",Toast.LENGTH_SHORT).show();
        }
    }
    //做重复选择
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void onTabChanged(String tag) {
        int count = tabHost.getTabWidget().getTabCount();
        for(int i=0;i<count;i++){
            View v = tabHost.getTabWidget().getChildAt(i);
            if(i == tabHost.getCurrentTab()){
                v.setSelected(true);
            }else
                v.setSelected(false);
        }
    }
}
